package com.yang.service.adminimpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yang.bean.*;
import com.yang.bean.common.BasePageInfo;
import com.yang.bean.common.CommonData;
import com.yang.bean.fxy.bo.IdsBo;
import com.yang.bean.yvo.PasswordBO;
import com.yang.mapper.*;
import com.yang.service.admin.AdminUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminUserServiceImpl
 * @Description:用户管理模块的接口加上行政区域和品牌制造商请求
 * @Author TrueEnd
 * @Date 2022/9/7
 * @Version
 **/
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketAddressMapper marketAddressMapper;
    @Autowired
    MarketCollectMapper marketCollectMapper;
    @Autowired
    MarketFootPrintMapper marketFootPrintMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;
    @Autowired
    MarketBrandMapper marketBrandMapper;
    @Autowired
    MarketRegionMapper marketRegionMapper;
    @Autowired
    MarketNoticeAdminMapper marketNoticeAdminMapper;


    @Override
    public CommonData<MarketUser> userList(String username, String mobile, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketUserExample marketUserExample = new MarketUserExample();
        marketUserExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%"+username+"%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileLike("%"+mobile+"%");
        }
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);
        PageInfo<MarketUser> pageInfo = new PageInfo<>(marketUsers);

        return CommonData.data(pageInfo);

    }

    @Override
    public MarketUser userDetail(Integer id) {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);
        return marketUser;
    }

    @Override
    public void userUpdate(MarketUser user) {
        marketUserMapper.updateByPrimaryKey(user);
    }


    @Override
    public CommonData<MarketAddress> addressList(String name, Integer userId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        marketAddressExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        PageInfo<MarketAddress> pageInfo = new PageInfo<>(marketAddresses);
        return CommonData.data(pageInfo);

    }

    @Override
    public CommonData<MarketCollect> collectList(Integer userId, Integer valueId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketCollectExample marketCollectExample = new MarketCollectExample();
        marketCollectExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketCollectExample.Criteria criteria = marketCollectExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }
        List<MarketCollect> marketCollects = marketCollectMapper.selectByExample(marketCollectExample);
        PageInfo<MarketCollect> pageInfo = new PageInfo<>(marketCollects);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFootPrint> FootprintList(Integer userId, Integer goodsId, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketFootPrintExample marketFootPrintExample = new MarketFootPrintExample();
        marketFootPrintExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketFootPrintExample.Criteria criteria = marketFootPrintExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null && !"".equals(goodsId)) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<MarketFootPrint> marketFootPrints = marketFootPrintMapper.selectByExample(marketFootPrintExample);
        PageInfo<MarketFootPrint> pageInfo = new PageInfo<>(marketFootPrints);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketSearchHistory> HistoryList(Integer userId, String keyword, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%"+keyword+"%");
        }
        List<MarketSearchHistory> marketSearchHistories = marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);
        PageInfo<MarketSearchHistory> pageInfo = new PageInfo<>(marketSearchHistories);
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketFeedback> FeedbackList(String username, Integer id, BasePageInfo info) {
        MarketFeedbackExample marketFeedbackExample = new MarketFeedbackExample();
        marketFeedbackExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketFeedbackExample.Criteria criteria = marketFeedbackExample.createCriteria();
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%"+username+"%");
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        List<MarketFeedback> marketFeedbacks = marketFeedbackMapper.selectByExample(marketFeedbackExample);
        PageInfo<MarketFeedback> pageInfo = new PageInfo<>(marketFeedbacks);
        return CommonData.data(pageInfo);
    }


    @Override
    public CommonData<MarketRegion> regionList() {
        MarketRegionExample l1MarketRegionExample = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = l1MarketRegionExample.createCriteria();
        byte l1Type = 1;
        criteria.andTypeEqualTo(l1Type);
        List<MarketRegion> marketRegions = marketRegionMapper.selectByExample(l1MarketRegionExample);
        PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions);
        for (MarketRegion l1MarketRegion : marketRegions) {
            Integer l2Id = l1MarketRegion.getId();
            byte l2Type = 2;
            MarketRegionExample l2MarketRegionExample = new MarketRegionExample();
            l2MarketRegionExample.createCriteria().andPidEqualTo(l2Id).andTypeEqualTo(l2Type);
            List<MarketRegion> l2MarketRegions = marketRegionMapper.selectByExample(l2MarketRegionExample);
            for (MarketRegion l2MarketRegion : l2MarketRegions) {
                Integer l3Id = l2MarketRegion.getId();
                byte l3Type = 3;
                MarketRegionExample l3MarketRegionExample = new MarketRegionExample();
                l3MarketRegionExample.createCriteria().andTypeEqualTo(l3Type).andPidEqualTo(l3Id);
                List<MarketRegion> l3MarketRegions = marketRegionMapper.selectByExample(l3MarketRegionExample);
                l2MarketRegion.setChildren(l3MarketRegions);
            }
            l1MarketRegion.setChildren(l2MarketRegions);
        }
        return CommonData.data(pageInfo);
    }

    @Override
    public CommonData<MarketBrand> brandList(Integer id, String name, BasePageInfo info) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        if (id != null && !"".equals(id)) {
            criteria.andIdEqualTo(id);
        }
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(marketBrandExample);
        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        return CommonData.data(pageInfo);

    }


    @Override
    public void brandDelete(MarketBrand brand) {
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = marketBrandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        marketBrandMapper.deleteByExample(marketBrandExample);
    }

    @Override
    public MarketBrand brandUpdate(MarketBrand brand) {
        marketBrandMapper.updateByPrimaryKey(brand);
        return brand;
    }

    @Override
    public CommonData<MarketNoticeAdmin> profileLsnotice(String title, String type, BasePageInfo info) {
        MarketNoticeAdminExample marketNoticeAdminExample = new MarketNoticeAdminExample();
        marketNoticeAdminExample.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketNoticeAdminExample.Criteria criteria = marketNoticeAdminExample.createCriteria();
        if(title!=null&&!"".equals(title)){
            criteria.andNoticeTitleLike(title);
        }
        if("unread".equals(type)){
            criteria.andReadTimeIsNull();
        }
        if("read".equals(type)){
            criteria.andReadTimeIsNotNull();
        }

        List<MarketNoticeAdmin> marketNoticeAdmins = marketNoticeAdminMapper.selectByExample(marketNoticeAdminExample);
        PageInfo<MarketNoticeAdmin> pageInfo=new PageInfo<>(marketNoticeAdmins);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketBrand brandCreate(MarketBrand brand) {
        brand.setAddTime(new Date());
        marketBrandMapper.insertSelective(brand);
        MarketBrand marketBrand = marketBrandMapper.selectByPrimaryKey(brand.getId());
        return marketBrand;
    }

    @Override
    public void profileCatnotice(MarketNoticeAdmin noticeAdmin) {
        noticeAdmin.setReadTime(new Date());
        MarketNoticeAdminExample marketNoticeAdminExample = new MarketNoticeAdminExample();
        MarketNoticeAdminExample.Criteria criteria = marketNoticeAdminExample.createCriteria();
        criteria.andIdEqualTo(noticeAdmin.getId());
        marketNoticeAdminMapper.updateByExampleSelective(noticeAdmin, marketNoticeAdminExample);
    }

    @Override
    public void profileBrmnotice(IdsBo ids) {
        for (Integer id : ids.getIds()) {
            marketNoticeAdminMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void profileBcatnotice(IdsBo ids) {
        for (Integer id : ids.getIds()) {
            MarketNoticeAdmin marketNoticeAdmin = new MarketNoticeAdmin();
            marketNoticeAdmin.setReadTime(new Date());
            MarketNoticeAdminExample marketNoticeAdminExample = new MarketNoticeAdminExample();
            MarketNoticeAdminExample.Criteria criteria = marketNoticeAdminExample.createCriteria();
            criteria.andIdEqualTo(id);
            marketNoticeAdminMapper.updateByExampleSelective(marketNoticeAdmin, marketNoticeAdminExample);
        }

    }


    /**
    * @description: admin的密码的修改
    [passwordBO, adminMsg]
    * @return: void
    * @author: 杨庆
    * @time: 2022.9.12 21:08
     */
    @Autowired
    MarketAdminMapper marketAdminMapper;
    @Override
    public void updatePassword(PasswordBO passwordBO, MarketAdmin adminMsg) {
        if (!StringUtils.isEmpty(passwordBO.getNewPassword())) {
           adminMsg.setPassword(passwordBO.getNewPassword());
        }
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(adminMsg.getId());
        marketAdminMapper.updateByExampleSelective(adminMsg,example);
    }


}
